<template>
	<view class="page-body">
		<view v-show="!loading">
			<el-button type="text" disabled>自定义按钮是否开启：</el-button>
			<el-switch v-model="isOpen" :active-value="1" :inactive-value="0"></el-switch>
			<view v-show="isOpen" style="margin-top: 40px;">
				<view style="display: flex;flex-direction: row;align-items: center;">
					<el-button type="text" disabled>按钮名称：</el-button>
					<el-input style="width: 200px;" v-model="btnName" placeholder="请输入内容"></el-input>
				</view>
				<view style="display: flex;flex-direction: row;align-items: center;margin-top: 10px;">
					<el-button type="text" disabled>解锁后按钮名称：</el-button>
					<el-input style="width: 200px;" v-model="afterBtnName" placeholder="请输入内容"></el-input>
				</view>
				<view style="display: flex;flex-direction: row;align-items: center;margin-top: 10px;">
					<el-button type="text" disabled>解锁后按钮跳转链接或微信小程序appID：</el-button>
					<el-input style="width: 200px;" v-model="afterBtnContent" placeholder="请输入内容"></el-input>
				</view>
				<view style="display: flex;flex-direction: row;align-items: center;margin-top: 40px;">
					<el-button type="text" disabled>开屏广告设置：</el-button>
					<el-radio-group v-model="adType">
						<el-radio :label="0">关闭广告</el-radio>
						<el-radio :label="1">微信开屏广告</el-radio>
						<el-radio :label="2">uniAD开屏广告</el-radio>
						<el-radio :label="3">自定义开屏广告</el-radio>
					</el-radio-group>
				</view>
				<view style="color: #999;margin-top: 20px;">
					<view v-if="adType == 0">
						关闭广告后将不展示开屏广告，请在相应小程序管理后台关闭
					</view>
					<view v-else-if="adType == 1">
						请在微信小程序管理后台设置开屏广告
					</view>
					<view v-else-if="adType == 2">
						请在uniapp小程序管理后台设置开屏广告
					</view>
					<view v-else-if="adType == 3">
						<view style="display: flex;flex-direction: column;margin-top: 40px;">
							<view style="display: flex;flex-direction: row;align-items: center;">
								<el-button type="text" disabled>上传海报：</el-button>
								<vk-data-upload v-model="adImg" :limit="1"></vk-data-upload>
							</view>
							<view style="display: flex;flex-direction: row;align-items: center;margin-top: 20px;">
								<el-button type="text" disabled>显示秒数：</el-button>
								<vk-data-input-number-box v-model="adSecond" :precision="0" :step="1" width="200px"
									placeholder="请输入数字"></vk-data-input-number-box>
							</view>
							<view style="display: flex;flex-direction: row;align-items: center;margin-top: 20px;">
								<el-button type="text" disabled>跳转链接或微信小程序appID：</el-button>
								<el-input style="width: 500px;" v-model="ad2url" placeholder="请输入内容"></el-input>
							</view>
						</view>
					</view>
				</view>

				<view style="display: flex;flex-direction: row;align-items: center;margin-top: 40px;">
					<el-button type="text" disabled>激励广告设置：</el-button>
					<el-radio-group v-model="jiliAdType" @input="jiliadChange()">
						<el-radio :label="0">关闭激励广告</el-radio>
						<el-radio :label="1">微信激励广告</el-radio>
						<el-radio :label="2">uniAD激励广告</el-radio>
					</el-radio-group>
				</view>
				<view style="color: #999;margin-top: 20px;">
					<view v-if="jiliAdType == 0">
					</view>
					<view v-else-if="jiliAdType == 1">
						<el-input style="width: 500px;" v-model="jiliWxAdID" placeholder="请输入内容"></el-input>
					</view>
					<view v-else-if="jiliAdType == 2">
						<el-input style="width: 500px;" v-model="jiliUniAdID" placeholder="请输入内容"></el-input>
					</view>
				</view>
				
			</view>
		</view>
		<view style="display: flex;flex-direction: row;align-items: center;margin-top: 40px;">
			<el-button type="text" disabled>首页背景图设置：</el-button>
			<vk-data-upload v-model="launchBgImg" :limit="1"></vk-data-upload>
		</view>
		<view style="display: flex;flex-direction: row;align-items: center;margin-top: 40px;">
			<el-button type="text" disabled>首页banner广告设置：</el-button>
			<el-radio-group v-model="bannerAdType" @input="bannerAdChange()">
				<el-radio :label="0">关闭首页banner广告</el-radio>
				<el-radio :label="1">微信首页banner广告</el-radio>
				<el-radio :label="2">uniAD首页banner广告</el-radio>
			</el-radio-group>
		</view>
		<view style="color: #999;margin-top: 20px;">
			<view v-if="bannerAdType == 0">
			</view>
			<view v-else-if="bannerAdType == 1">
				<el-input style="width: 500px;" v-model="bannerAdID" placeholder="请输入内容"></el-input>
			</view>
			<view v-else-if="bannerAdType == 2">
				<el-input style="width: 500px;" v-model="bannerAdID" placeholder="请输入内容"></el-input>
			</view>
		</view>
		
		<view style="display: flex;flex-direction: row;align-items: center;margin-top: 40px;">
			<el-button type="text" disabled>制作头像激励广告设置：</el-button>
			<el-radio-group v-model="avatarJiliAdType" @input="avatarJiliadChange()">
				<el-radio :label="0">关闭激励广告</el-radio>
				<el-radio :label="1">微信激励广告</el-radio>
				<el-radio :label="2">uniAD激励广告</el-radio>
			</el-radio-group>
		</view>
		<view style="color: #999;margin-top: 20px;">
			<view v-if="avatarJiliAdType == 0">
			</view>
			<view v-else-if="avatarJiliAdType == 1">
				<el-input style="width: 500px;" v-model="avatarJiliAdID" placeholder="请输入内容"></el-input>
			</view>
			<view v-else-if="avatarJiliAdType == 2">
				<el-input style="width: 500px;" v-model="avatarJiliAdID" placeholder="请输入内容"></el-input>
			</view>
		</view>
		<el-button @click="saveConfig()" style="margin-top: 40px;" type="primary">保存</el-button>
	</view>
</template>

<script>
	var that; // 当前页面对象
	var vk = uni.vk; // vk实例
	var originalForms = {}; // 表单初始化数据
	var action = "admin/ad/ad/sys"
	export default {
		data() {
			// 页面数据变量
			return {
				loading: false,
				id: null,
				isOpen: 1,
				btnName: '',
				afterBtnName: '',
				afterBtnContent: '',
				adType: 0,
				adImg: null,
				adSecond: 60,
				ad2url: null,
				jiliAdType: 0,
				jiliWxAdID: null,
				jiliUniAdID: null,
				launchBgImg:null,
				bannerAdType: 0,
				bannerAdID: null,
				avatarJiliAdType: 0,
				avatarJiliAdID: null,
				// 页面是否请求中或加载中
				loading: false,
				formDatas: {},
				// 表单相关结束 -----------------------------------------------------------
			};
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		onLoad(options = {}) {
			that = this;
			vk = that.vk;
			that.options = options;
			
			this.loading = true
			vk.callFunction({
				url: 'admin/adConfig/ad/sys/getList',
				title: '请求中...',
				data: {
			
				},
				success: (data) => {
					this.loading = false
					console.log("----", data)
					if (data.code == 0) {
						var rows = data.rows
						if (rows.length == 0) {
							this.id = null
						} else {
							for (var i in rows) {
								var tmp = rows[i]
								this.id = tmp._id
								this.isOpen = tmp.isOpen
								this.btnName = tmp.btnName
								this.adType = tmp.adType
								if (this.adType == 3) {
									this.adImg = tmp.kaipingAd.adImg
									this.adSecond = tmp.kaipingAd.adSecond
									this.ad2url = tmp.kaipingAd.ad2url
								}
								this.jiliAdType = tmp.jiliAdType
								if (this.jiliAdType == 1) {
									this.jiliWxAdID = tmp.jiliAdID
								} else {
									this.jiliUniAdID = tmp.jiliAdID
								}
								this.afterBtnName = tmp.afterBtnName
								this.afterBtnContent = tmp.afterBtnContent
								
								this.launchBgImg = tmp.launchBgImg
								this.bannerAdType = tmp.bannerAdType
								this.bannerAdID = tmp.bannerAdID
								this.avatarJiliAdType = tmp.avatarJiliAdType
								this.avatarJiliAdID = tmp.avatarJiliAdID
								break
							}
						}
					}
				},
				fail: (err) => {
			
				},
				complete: (res) => {
			
				}
			});
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady() {},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {
			
		},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {},
		// 函数
		methods: {
			jiliadChange() {
				console.log("----")
			},
			bannerAdChange() {
				this.bannerAdID = null
			},
			avatarJiliadChange() {
				this.avatarJiliAdID = null
			},
			saveConfig() {
				if (this.isOpen == 1) {
					if (!this.btnName || this.btnName.length == 0) {
						vk.toast("请输入按钮名称");
						return
					}
					if (!this.afterBtnName || this.afterBtnName.length == 0) {
						vk.toast("请输入解锁后按钮名称");
						return
					}
					if (!this.afterBtnContent || this.afterBtnContent.length == 0) {
						vk.toast("请输入解锁后按钮跳转内容");
						return
					}
					if (this.adType == 3) {
						if (!this.adImg || this.adImg.length == 0) {
							vk.toast("请上传海报");
							return
						}
						if (!this.ad2url || this.ad2url.length == 0) {
							vk.toast("请输入跳转链接");
							return
						}
					}
					if (this.jiliAdType == 1) {
						if (!this.jiliWxAdID || this.jiliWxAdID.length == 0) {
							vk.toast("请输入广告id");
							return
						}
					}
					if (this.jiliAdType == 2) {
						if (!this.jiliUniAdID || this.jiliUniAdID.length == 0) {
							vk.toast("请输入广告id");
							return
						}
					}
				}
				if(this.bannerAdType != 0 ){
					if (!this.bannerAdID || this.bannerAdID.length == 0) {
						vk.toast("请输入banner广告id");
						return
					}
				}
				if(this.avatarJiliAdType != 0 ){
					if (!this.avatarJiliAdID || this.avatarJiliAdID.length == 0) {
						vk.toast("请输入激励广告id");
						return
					}
				}
				
				if (this.id) {
					vk.callFunction({
						url: 'admin/adConfig/ad/sys/update',
						title: '请求中...',
						data: {
							'_id': this.id,
							'isOpen': this.isOpen,
							'btnName': this.btnName,
							'afterBtnName': this.afterBtnName,
							'afterBtnContent': this.afterBtnContent,
							'adType': this.adType,
							'kaipingAd': {
								'adImg': this.adImg,
								'adSecond': this.adSecond,
								'ad2url': this.ad2url,
							},
							'jiliAdType': this.jiliAdType,
							'jiliAdID': this.jiliAdType == 1 ? this.jiliWxAdID : this.jiliUniAdID,
							'launchBgImg': this.launchBgImg,
							'bannerAdType': this.bannerAdType,
							'bannerAdID': this.bannerAdID ,
							'avatarJiliAdType': this.avatarJiliAdType,
							'avatarJiliAdID': this.avatarJiliAdID ,
						},
						success: (data) => {

						},
						fail: (err) => {

						},
						complete: (res) => {

						}
					});
				} else {
					vk.callFunction({
						url: 'admin/adConfig/ad/sys/add',
						title: '请求中...',
						data: {
							'isOpen': this.isOpen,
							'btnName': this.btnName,
							'adType': this.adType,
							'kaipingAd': {
								'adImg': this.adImg,
								'adSecond': this.adSecond,
								'ad2url': this.ad2url,
							},
							'jiliAdType': this.jiliAdType,
							'jiliAdID': this.jiliAdType == 1 ? this.jiliWxAdID : this.jiliUniAdID
						},
						success: (data) => {

						},
						fail: (err) => {

						},
						complete: (res) => {

						}
					});
				}

			},
			// 页面数据初始化函数
			init(options) {

			},
			// 页面跳转
			pageTo(path) {
				vk.navigateTo(path);
			},
			// 表单重置
			resetForm() {
				vk.pubfn.resetForm(originalForms, that);
			},
			// 搜索
			search() {
				that.$refs.table1.search();
			},
			// 刷新
			refresh() {
				that.$refs.table1.refresh();
			},
			// 获取当前选中的行的数据
			getCurrentRow() {
				return that.$refs.table1.getCurrentRow();
			},
			// 监听 - 行的选中高亮事件
			currentChange(val) {
				that.table1.selectItem = val;
			},
			// 当选择项发生变化时会触发该事件
			selectionChange(list) {
				that.table1.multipleSelection = list;
			},
			// 显示添加页面
			addBtn() {
				that.resetForm();
				that.form1.props.action = action + '/add';
				that.form1.props.formType = 'add';
				that.form1.props.title = '添加';
				that.form1.props.show = true;
			},

			// 显示修改页面
			updateBtn({
				item
			}) {
				that.form1.props.action = action + '/update';
				that.form1.props.formType = 'update';
				that.form1.props.title = '编辑';
				that.form1.props.show = true;
				that.form1.data = item

			},
			// 删除按钮
			deleteBtn({
				item,
				deleteFn
			}) {
				deleteFn({
					action: action + "/delete",
					data: {
						_id: item._id
					},
				});
			},
			// 批量删除
			delete(ids) {
				let data = {

				};
				if (vk.pubfn.isNull(ids)) {
					ids = vk.pubfn.arrayObjectGetArray(that.table1.multipleSelection, "_id");
				}

				if (vk.pubfn.isArray(ids)) {
					data["ids"] = ids;
				} else {
					data["ids"] = [ids];
				}
				vk.callFunction({
					url: action + '/delete',
					title: '请求中...',
					data,
					success: function(data) {
						that.refresh();
					}
				});
			},
			// 监听 - 批量操作的按钮点击事件
			batchBtn(index) {
				switch (index) {
					case 1:
						this.delete()
						break;
					default:
						break;
				}
			}
		},
		// 监听属性
		watch: {

		},
		// 过滤器
		filters: {

		},
		// 计算属性
		computed: {

		}
	};
</script>
<style lang="scss" scoped>
	.page-body {}
</style>